package com.uit.se6.mydiary.note;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class NotesDbAdapter {
	public static final String DATABASE_NAME = "diary_data";

	private static final String TAG = "NotesDbAdapter";
	private SQLiteDatabase mDb;
	private DatabaseHelper mDbHelper;
	private final Context mCtx;

	private static final String DATABASE_TABLE = "notes";
	private static final int DATABASE_VERSION = 1;

	public static final String KEY_BODY = "body";
	public static final String KEY_ID = "id";
	private static final String DATABASE_CREATE = "create table "
			+ DATABASE_TABLE + " (" + KEY_ID + " integer primary key, "
			+ KEY_BODY + " text not null);";

	public static final String KEY2_IMAGEID = "images_id";
	public static final String KEY2_IMAGEPATH = "images";
	private static final String DATABASE_TABLE2 = "table_images";
	private static final String DATABASE_CREATE2 = "create table "
			+ DATABASE_TABLE2 + " (" + KEY2_IMAGEID
			+ " integer primary key autoincrement, " + KEY2_IMAGEPATH
			+ " text not null, " + KEY_ID + " integer not null);";

	private static class DatabaseHelper extends SQLiteOpenHelper {
		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE);
			db.execSQL(DATABASE_CREATE2);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("Drop table if exists " + DATABASE_TABLE);
			db.execSQL("Drop table if exists " + DATABASE_TABLE2);
			onCreate(db);
		}
	};

	public NotesDbAdapter(Context context) {
		mCtx = context;
	}

	public NotesDbAdapter open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	public boolean createNote(int idNote, String body) {
		ContentValues values = setValue(idNote, body);
		long tempLong = mDb.insert(DATABASE_TABLE, null, values);
		if (tempLong == -1)
			return false;
		return true;
	}

	/*
	 * false if fail
	 */
	public boolean createImage(String imgPath, int noteID) {
		ContentValues values = setValue(imgPath, noteID);
		long temp = mDb.insert(DATABASE_TABLE2, null, values);
		if (temp == -1)
			return false;
		return true;
	}

	public boolean deleteNote(int id) {
		return mDb.delete(DATABASE_TABLE, KEY_ID + " = " + id, null) > 0;
	}

	public boolean deleteImage(String path, int noteId) {
		try {
			return mDb.delete(DATABASE_TABLE2, KEY2_IMAGEPATH + " = ?"
					+ " AND " + KEY_ID + " = " + noteId, new String[] { path }) > 0;
		} catch (Exception ex) {
			Log.e("DELETE ROW in " + DATABASE_TABLE2 + " EXCEPTION",
					ex.toString());
			return false;
		}
	}

	public boolean deleteAllImageByNote(int noteId) {
		try {
			return mDb.delete(DATABASE_TABLE2, KEY_ID + " = " + noteId, null) > 0;
		} catch (Exception ex) {
			Log.e("DELETE ROW in " + DATABASE_TABLE2 + " EXCEPTION",
					ex.toString());
			return false;
		}
	}

	public boolean deleteAll() {
		return (mDb.delete(DATABASE_TABLE, null, null)) > 0
				|| (mDb.delete(DATABASE_TABLE2, null, null)) > 0;
	}

	public Cursor fetchAllNotes() {
		return mDb.query(DATABASE_TABLE, new String[] { KEY_ID, KEY_BODY },
				null, null, null, null, null);
	}

	public Cursor fetchAllImageString() {
		return mDb.query(true, DATABASE_TABLE2,
				new String[] { KEY2_IMAGEPATH }, null, null, null, null, null,
				null);
	}

	public Cursor fetchImageByNote(int noteId) {
		return mDb.query(true, DATABASE_TABLE2,
				new String[] { KEY2_IMAGEPATH }, KEY_ID + " = " + noteId, null,
				null, null, null, null);
	}

	public Cursor fetchImageByPath(String path) {
		return mDb.query(true, DATABASE_TABLE2,
				new String[] { KEY2_IMAGEPATH }, KEY2_IMAGEPATH + " = ?",
				new String[] { path }, null, null, null, null);
	}

	/**
	 * get notes of a month Ex: startValue = 201409
	 * 
	 * @param startValue
	 * @return
	 */
	public Cursor fetchNoteStartWith(int startValue) {
		return mDb.query(true, DATABASE_TABLE,
				new String[] { KEY_ID, KEY_BODY }, KEY_ID + "/100 = "
						+ startValue, null, null, null, null, null);
	}

	public Cursor fetchNote(int id) throws SQLException {
		Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] { KEY_ID,
				KEY_BODY }, KEY_ID + " = " + id, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public boolean updateNote(int noteId, String body) {
		ContentValues values = setValue(noteId, body);
		return mDb
				.update(DATABASE_TABLE, values, KEY_ID + " = " + noteId, null) > 0;
	}

	private ContentValues setValue(int noteId, String body) {
		ContentValues values = new ContentValues();
		values.put(KEY_ID, noteId);
		values.put(KEY_BODY, body);
		return values;
	}

	private ContentValues setValue(String imgPath, int noteId) {
		ContentValues values = new ContentValues();
		values.put(KEY2_IMAGEPATH, imgPath);
		values.put(KEY_ID, noteId);
		return values;
	}
}